﻿using ToolKitManagerIESiteList.Util;
using System;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics.CodeAnalysis;

namespace ToolKitManagerIESiteList
{
    public partial class FrmRemoveDomains : Form
    {

        [SuppressMessage("Microsoft.Performance", "CA1819")]
        public string[] DataRemoveDomains { get; set; }
        [SuppressMessage("Microsoft.Performance", "CA1819")]
        public string[] ResultRemoveDomains { get; private set; }
        public FrmRemoveDomains()
        {
            InitializeComponent();
        }

        private void LnkEditDomain_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            using (var frm = new FrmEditFilterDomains())
            {
                frm.ShowDialog(this);
            }
            var excludeDomains = Properties.Settings.Default.ExcludeDomains
                .Cast<string>()
                .ToArray();
            for (int i = 0; i < ChkLstDomains.Items.Count; i++)
            {
                if (!excludeDomains.Contains(ChkLstDomains.Items[i].ToString()))
                {
                    ChkLstDomains.SetItemChecked(i, false);
                }
            }
        }
        private void LnkFilterDomain_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var excludeDomains = Properties.Settings.Default.ExcludeDomains
                .Cast<string>()
                .ToList();
            var qtd = 0;
            for (int i = 0; i < ChkLstDomains.Items.Count; i++)
            {
                if (ChkLstDomains.GetItemChecked(i))
                {
                    if (!excludeDomains.Contains(ChkLstDomains.Items[i].ToString()))
                    {
                        qtd++;
                        excludeDomains.Add(ChkLstDomains.Items[i].ToString());
                    }
                }
            }
            StringCollection collection = new StringCollection();
            collection.AddRange(excludeDomains.ToArray());
            Properties.Settings.Default.ExcludeDomains = collection;
            Properties.Settings.Default.Save();
            if (qtd > 0)
            {
                this.ShowInformation(string.Format(Properties.Resources.AddedFilters, qtd), AssemblyHelper.AssemblyTitle);
            }
        }

        private void BntClose_Click(object sender, EventArgs e)
        {
            ResultRemoveDomains = new string[] { };
            this.DialogResult = DialogResult.Cancel;
            Close();
        }

        private void FrmRemoveDomains_Load(object sender, EventArgs e)
        {
            ChkLstDomains.Items.Clear();
            foreach (var item in DataRemoveDomains.OrderBy(d => d.ToLower()))
            {
                ChkLstDomains.Items.Add(item);
            }
        }

        private void BntRemove_Click(object sender, EventArgs e)
        {
            if (ChkLstDomains.CheckedItems.Count == 0)
            {
                this.ShowWarning(Properties.Resources.SelectOneMoreDomain, AssemblyHelper.AssemblyTitle);
                return;
            }
            ResultRemoveDomains = ChkLstDomains.CheckedItems.Cast<string>().ToArray();
            this.DialogResult = DialogResult.OK;
            Close();
        }
    }
}
